var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  return value;
};
class ImView {
  constructor(options) {
    __publicField(this, "options", {
      notification: false,
      iframe: "",
      parentPostMessageType: "parentMessage",
      listenerPostMessageType: "imMessage"
    });
    __publicField(this, "version", "1.0.0");
    window.ImView = ImView;
    console.log(options);
    if (options) {
      this.options = Object.assign(this.options, options);
    }
    console.log(this.options);
  }
  IframeOnLoad() {
    console.log("iframe is load");
    this.BrowserNotification();
    window.addEventListener("message", (evt) => {
      console.log("\u53EF\u4EE5\u8FDB\u5165\u5206\u652F\uFF1A", evt.data.type === this.options.listenerPostMessageType);
      if (evt.data.type === this.options.listenerPostMessageType) {
        console.log("\u76D1\u542C\u5230\u901A\u77E5", evt.data);
        if (this.options.notification) {
          this.BrowserNotification(evt.data.msg);
        } else {
          this.PostUrlToIframe();
        }
        this.DocumentTitleFlashing();
      }
    });
  }
  DocumentTitleFlashing() {
    let title = document.title;
    let timer = null;
    let count = 0;
    function change() {
      document.title = (count % 2 === 0 ? "" : "\u3010\u65B0\u6D88\u606F\u3011") + "\u60A8\u6709\u4E00\u6761\u5BA2\u670D\u6D88\u606F";
      count++;
      if (count <= 5) {
        timer = setTimeout(change, 500);
      } else {
        clearTimeout(timer);
        timer = null;
        document.title = title;
        count = 0;
      }
    }
    change();
  }
  BrowserNotification(msg) {
    if (window.Notification) {
      window.Notification.requestPermission((status) => {
        if (Notification.permission === "granted" && msg) {
          const n = new Notification("\u65B0\u6D88\u606F\u901A\u77E5", { tag: "imMessage", body: msg });
          setTimeout(n.close(n), 3e3);
          n.onclick = () => {
            window.open(location.href);
          };
        }
      });
    }
  }
  PostUrlToIframe() {
    setTimeout(() => {
      document.getElementById(this.options.iframe).contentWindow.postMessage({ type: this.options.parentPostMessageType, url: location.href }, "*");
    }, 0);
  }
}
export { ImView as default };
